package cn.swust.designpatternlearningplatform.dao;

import cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate;
import cn.swust.designpatternlearningplatform.domain.entity.TopicWrongPointLink;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;
import java.util.Set;

@Mapper
public interface TopicUpdateDao {
    /**
     * 插入用户题目数据进topic_update
     *
     * @param topicUpdate 所要插入的数据
     * @return void
     * @author Tang
     * @date 2023/4/15 23:47
     **/
    @Insert("INSERT INTO " + "topic_update(student_number, topic_id, update_time, update_type,topic_type) " + "VALUE('${topic.studentNumber}','${topic.topicId}','${topic.updateTime}','${topic.updateType}','${topic.topicType}')")
    void insertTopicUpdate(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 批量插入用户题目数据
     *
     * @param topicUpdates
     * @return void
     * @author Tang
     * @date 2023/9/23 10:50
     **/
    void insertTopicUpdateBatch(@Param("list") List<TopicUpdate> topicUpdates);

    /**
     * 删除用户题目数据
     *
     * @param topicUpdate
     * @return void
     * @author Tang
     * @date 2023/4/16 2:05
     **/
    @Delete("DELETE FROM topic_update " + "WHERE student_number = '${topic.studentNumber}' " + "and topic_id = '${topic.topicId}' " + "and update_type = '${topic.updateType}'")
    void deleteTopicUpdate(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 返回对应的题目数据元
     *
     * @param topicUpdate 用于查询数据的变量
     * @return cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate
     * @author Tang
     * @date 2023/4/15 23:48
     **/
    @Select("SELECT " + "id, student_number, topic_id, update_time, update_type " + "FROM topic_update " + "WHERE student_number = '${topic.studentNumber}' " + "AND topic_id = '${topic.topicId}' " + "AND update_type = '${topic.updateType}'")
    TopicUpdate selectTopicUpdate(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 分页查询某个类型（wrong，shield，collect）的题目数据元
     *
     * @param topicUpdate
     * @param current
     * @param size
     * @return java.util.List<cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate>
     * @author Tang
     * @date 2023/4/20 19:42
     **/
    List<TopicUpdate> selectTopicUpdateByStudent(@Param("topic") TopicUpdate topicUpdate, @Param("current") Integer current, @Param("size") Integer size);

    /**
     * 根据知识点id分页查询某个类型（wrong，shield，collect）的题目数据元
     *
     * @param pointId
     * @param topicUpdate
     * @param current
     * @param size
     * @return java.util.List<cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate>
     * @author Tang
     * @date 2023/10/9 21:30
     **/
    List<TopicUpdate> selectTopicUpdateByPointIdAndStudent(@Param("pointId") Integer pointId, @Param("topic") TopicUpdate topicUpdate, @Param("current") Integer current, @Param("size") Integer size);

    /**
     * 获取某个类型题目元的总数
     *
     * @param topicUpdate
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/7/17 17:39
     **/
    Integer selectTopicUpdateByStudentTotal(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 获取用户所对应的某个类型（wrong，shield，collect）下题目的总数量
     *
     * @param topicUpdate
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/4/20 19:42
     **/
    @Select("SELECT COUNT(DISTINCT (topic_id))\n" + "FROM topic_update\n" + "WHERE student_number = '${topic.studentNumber}'\n\n" + "  " + "AND update_type = '${topic.updateType}'")
    Integer countTopicUpdateByStudent(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 获取用户的所有的题目id
     *
     * @param topicUpdate
     * @return java.util.List<java.lang.Integer>
     * @author Tang
     * @date 2023/4/23 21:20
     **/
    @Select("SELECT DISTINCT topic_id " + "FROM topic_update " + "WHERE student_number = '${topic.studentNumber}' " + "AND update_type = '${topic.updateType}'")
    List<Integer> selectAllByTopicType(@Param("topic") TopicUpdate topicUpdate);

    /**
     * 查询用户某知识点下的错题数量
     *
     * @param studentNumber
     * @param pointId
     * @return java.lang.Integer
     * @author Tang
     * @date 2023/7/4 20:42
     **/
    @Select("SELECT id,student_number,point_id,num FROM topic_wrong_point_link WHERE student_number = ${student} AND point_id = ${pointId}")
    TopicWrongPointLink selectWrongLinkNum(@Param("student") String studentNumber, @Param("pointId") Integer pointId);

    /**
     * 批量查询某知识点下的错题数量
     *
     * @param studentNumber
     * @param pointIds
     * @return cn.swust.designpatternlearningplatform.domain.entity.TopicWrongPointLink
     * @author Tang
     * @date 2023/9/23 10:12
     **/
    List<TopicWrongPointLink> selectWrongLinkNumByPointIds(@Param("student") String studentNumber, @Param("pointIds") List<Integer> pointIds);

    /**
     * 插入某用户某错误知识点信息
     *
     * @param studentNumber
     * @param pointId
     * @param num
     * @return void
     * @author Tang
     * @date 2023/7/4 20:42
     **/
    @Insert("INSERT INTO topic_wrong_point_link(STUDENT_NUMBER, POINT_ID, NUM) " + "VALUE ('${student}',${pointId}, ${num})")
    void insertStudentWrongPointLink(@Param("student") String studentNumber, @Param("pointId") Integer pointId, @Param("num") Integer num);

    /**
     * 批量更新学生错题信息
     *
     * @param topicWrongPointLinks
     * @return void
     * @author Tang
     * @date 2023/9/23 15:58
     **/
    void insertOrUpdateStudentWrongPointLinkBatch(@Param("list") List<TopicWrongPointLink> topicWrongPointLinks);

    /**
     * 更新新某用户某错误知识点信息
     *
     * @param studentNumber
     * @param pointId
     * @param num
     * @return void
     * @author Tang
     * @date 2023/7/4 20:42
     **/
    @Insert("UPDATE topic_wrong_point_link SET num = ${num} " + "WHERE student_number = ${studentNumber} AND point_id = ${pointId}")
    void updateStudentWrongPointLinkNum(@Param("student") String studentNumber, @Param("pointId") Integer pointId, @Param("num") Integer num);

    /**
     * 获取错误知识点信息列表
     *
     * @param studentNumber
     * @return java.util.List<cn.swust.designpatternlearningplatform.domain.entity.TopicWrongPointLink>
     * @author Tang
     * @date 2023/7/4 20:48
     **/
    @Select("SELECT student_number, point_id, num " + "FROM topic_wrong_point_link " + "WHERE student_number = ${student} " + "LIMIT ${current},${size}")
    List<TopicWrongPointLink> selectTopicWrongPointLink(@Param("student") String studentNumber, @Param("current") Integer current, @Param("size") Integer size);

    List<TopicUpdate> getCollectsByStuNumAndTopicIds(@Param("studentNumber") String studentNumber, @Param("topicIds") Collection<Integer> topicIds, @Param("typeName") String typeName);
}
